/**
 * Filename: SettingActivity.java
 *
 * @author Firefly
 * @contact postfirefly@163.com
 * @time 2016/3/9 2:36
 */
package com.github.postsanf.yinian.activity;

import android.os.Bundle;

import com.bumptech.glide.Glide;
import com.github.postsanf.yinian.BaseActivity;
import com.github.postsanf.yinian.R;
import com.github.postsanf.yinian.adapter.TimeCardpicBigAdapter;
import com.github.postsanf.yinian.bean.YNStatus;
import com.github.postsanf.yinian.constants.CommonConstants;
import com.github.postsanf.yinian.constants.YNSingleton;
import com.github.postsanf.yinian.utils.ReqUtils;
import com.github.postsanf.yinian.utils.YNUrlHelper;
import com.github.postsanf.yinian.view.LoopViewPager;
import com.litesuits.http.exception.HttpException;
import com.litesuits.http.listener.HttpListener;
import com.litesuits.http.request.StringRequest;
import com.litesuits.http.request.param.HttpMethods;
import com.litesuits.http.response.Response;
import com.umeng.analytics.MobclickAgent;

import org.json.JSONObject;

import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;

public class TimeCardBigPicActivity extends BaseActivity {
    //    private MyAdGallery2 myAdGallery;
    private List<String> tempUris=new ArrayList<>();
    private LoopViewPager adViewPager;
    private TimeCardpicBigAdapter timeCardpicbigAdapter;
    private YNStatus mCurStatus;

    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.yn_activity_timecard_bigpic);
        initDatas();
        initView();
    }

    private void initDatas() {
        mCurStatus = YNSingleton.getInstance().getCurStatus();
    }

    @Override
    public void onBackPressed() {
        super.onBackPressed();
        finish();
    }

    private void initView() {
        adViewPager = (LoopViewPager) findViewById(R.id.id_loopviewpager);
//        myAdGallery = (MyAdGallery2) findViewById(R.id.id_viewpager);
//        myAdGallery.start(this, tempUris, 5000);
//        myAdGallery.setOnItemClickListener(new AdapterView.OnItemClickListener() {
//            @Override
//            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
//                finish();
//            }
//        });

//        timeCardpicbigAdapter = new TimeCardpicBigAdapter(TimeCardBigPicActivity.this, tempUris);
//        adViewPager.setAdapter(timeCardpicbigAdapter);
        initUrl();
    }

    private void initUrl() {
        StringBuilder stringBuilder = new StringBuilder();
        for (int i = 0; i < mCurStatus.getUrl().size(); i++) {
            String url = mCurStatus.getUrl().get(i).substring(0, mCurStatus.getUrl().get(i).indexOf("?") + 1);
            String helpurl = YNUrlHelper.getTimeCardUrl(url, 480);
            stringBuilder.append(helpurl).append(",");
        }
        String posturl = stringBuilder.toString().substring(0, stringBuilder.toString().lastIndexOf(","));
        initposturl(posturl);
    }

    private void initposturl(String posturl) {
        LinkedHashMap<String, String> mParam = new LinkedHashMap<>();
        mParam.put("url", posturl);
        StringRequest req = new StringRequest(getString(R.string.BASE_URL) + "GetDownloadToken")
                .setMethod(HttpMethods.Post)
                .setParamMap(mParam)
                .setHttpListener(new HttpListener<String>(true, false, true) {
                    @Override
                    public void onSuccess(String s, Response<String> response) {
                        try {
                            JSONObject jsonObject = new JSONObject(s.toString());
                            if (ReqUtils.isSuccess(jsonObject.optString("code"))) {
                                String[] data = jsonObject.optJSONArray("data").optJSONObject(0).optString("url").split(",");
                                for (int i = 0; i < data.length; i++) {
                                    tempUris.add(data[i]);
                                }
                                timeCardpicbigAdapter = new TimeCardpicBigAdapter(TimeCardBigPicActivity.this, tempUris);
                                adViewPager.setAdapter(timeCardpicbigAdapter);
                            }
                        } catch (Exception e) {
                            e.printStackTrace();
                        }
                    }

                    @Override
                    public void onFailure(HttpException e, Response<String> response) {
                        showToast(CommonConstants.YN_NET_LINK);
                    }
                });
        liteHttp.executeAsync(req);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
//        myAdGallery.stopTimer();
        Glide.get(this).clearMemory();
        System.gc();
    }

    // 友盟统计
    public void onResume() {
        super.onResume();
        MobclickAgent.onPageStart(this.getClass().getSimpleName());
        MobclickAgent.onResume(this);
    }

    public void onPause() {
        super.onPause();
        MobclickAgent.onPageEnd(this.getClass().getSimpleName());
        MobclickAgent.onPause(this);
    }
}
